home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / multiprocessing / reduction.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  6KB  |  161 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. __all__ = []
  5. import os
  6. import sys
  7. import socket
  8. import threading
  9. import _multiprocessing
  10. from multiprocessing import current_process
  11. from multiprocessing.forking import Popen, duplicate, close, ForkingPickler
  12. from multiprocessing.util import register_after_fork, debug, sub_debug
  13. from multiprocessing.connection import Client, Listener
  14. if not sys.platform == 'win32' or hasattr(_multiprocessing, 'recvfd'):
  15.     raise ImportError('pickling of connections not supported')
  16. if sys.platform == 'win32':
  17.     import _subprocess
  18.     from _multiprocessing import win32
  19.     
  20.     def send_handle(conn, handle, destination_pid):
  21.         process_handle = win32.OpenProcess(win32.PROCESS_ALL_ACCESS, False, destination_pid)
  22.         
  23.         try:
  24.             new_handle = duplicate(handle, process_handle)
  25.             conn.send(new_handle)
  26.         finally:
  27.             close(process_handle)
  28.  
  29.  
  30.     
  31.     def recv_handle(conn):
  32.         return conn.recv()
  33.  
  34. else:
  35.     
  36.     def send_handle(conn, handle, destination_pid):
  37.         _multiprocessing.sendfd(conn.fileno(), handle)
  38.  
  39.     
  40.     def recv_handle(conn):
  41.         return _multiprocessing.recvfd(conn.fileno())
  42.  
  43. _cache = set()
  44.  
  45. def _reset(obj):
  46.     global _lock, _listener
  47.     for h in _cache:
  48.         close(h)
  49.     
  50.     _cache.clear()
  51.     _lock = threading.Lock()
  52.     _listener = None
  53.  
  54. _reset(None)
  55. register_after_fork(_reset, _reset)
  56.  
  57. def _get_listener():
  58.     global _listener
  59.     if _listener is None:
  60.         _lock.acquire()
  61.         
  62.         try:
  63.             if _listener is None:
  64.                 debug('starting listener and thread for sending handles')
  65.                 _listener = Listener(authkey = current_process().authkey)
  66.                 t = threading.Thread(target = _serve)
  67.                 t.daemon = True
  68.                 t.start()
  69.         finally:
  70.             _lock.release()
  71.  
  72.     return _listener
  73.  
  74.  
  75. def _serve():
  76.     is_exiting = is_exiting
  77.     sub_warning = sub_warning
  78.     import util
  79.     while None:
  80.         
  81.         try:
  82.             conn = _listener.accept()
  83.             (handle_wanted, destination_pid) = conn.recv()
  84.             _cache.remove(handle_wanted)
  85.             send_handle(conn, handle_wanted, destination_pid)
  86.             close(handle_wanted)
  87.             conn.close()
  88.         continue
  89.         if not is_exiting():
  90.             import traceback as traceback
  91.             sub_warning('thread for sharing handles raised exception :\n' + '-' * 79 + '\n' + traceback.format_exc() + '-' * 79)
  92.         
  93.  
  94.         continue
  95.         return None
  96.  
  97.  
  98. def reduce_handle(handle):
  99.     if Popen.thread_is_spawning():
  100.         return (None, Popen.duplicate_for_child(handle), True)
  101.     dup_handle = None(handle)
  102.     _cache.add(dup_handle)
  103.     sub_debug('reducing handle %d', handle)
  104.     return (_get_listener().address, dup_handle, False)
  105.  
  106.  
  107. def rebuild_handle(pickled_data):
  108.     (address, handle, inherited) = pickled_data
  109.     if inherited:
  110.         return handle
  111.     None('rebuilding handle %d', handle)
  112.     conn = Client(address, authkey = current_process().authkey)
  113.     conn.send((handle, os.getpid()))
  114.     new_handle = recv_handle(conn)
  115.     conn.close()
  116.     return new_handle
  117.  
  118.  
  119. def reduce_connection(conn):
  120.     rh = reduce_handle(conn.fileno())
  121.     return (rebuild_connection, (rh, conn.readable, conn.writable))
  122.  
  123.  
  124. def rebuild_connection(reduced_handle, readable, writable):
  125.     handle = rebuild_handle(reduced_handle)
  126.     return _multiprocessing.Connection(handle, readable = readable, writable = writable)
  127.  
  128. ForkingPickler.register(_multiprocessing.Connection, reduce_connection)
  129.  
  130. def fromfd(fd, family, type_, proto = 0):
  131.     s = socket.fromfd(fd, family, type_, proto)
  132.     if s.__class__ is not socket.socket:
  133.         s = socket.socket(_sock = s)
  134.     return s
  135.  
  136.  
  137. def reduce_socket(s):
  138.     reduced_handle = reduce_handle(s.fileno())
  139.     return (rebuild_socket, (reduced_handle, s.family, s.type, s.proto))
  140.  
  141.  
  142. def rebuild_socket(reduced_handle, family, type_, proto):
  143.     fd = rebuild_handle(reduced_handle)
  144.     _sock = fromfd(fd, family, type_, proto)
  145.     close(fd)
  146.     return _sock
  147.  
  148. ForkingPickler.register(socket.socket, reduce_socket)
  149. if sys.platform == 'win32':
  150.     
  151.     def reduce_pipe_connection(conn):
  152.         rh = reduce_handle(conn.fileno())
  153.         return (rebuild_pipe_connection, (rh, conn.readable, conn.writable))
  154.  
  155.     
  156.     def rebuild_pipe_connection(reduced_handle, readable, writable):
  157.         handle = rebuild_handle(reduced_handle)
  158.         return _multiprocessing.PipeConnection(handle, readable = readable, writable = writable)
  159.  
  160.     ForkingPickler.register(_multiprocessing.PipeConnection, reduce_pipe_connection)
  161.